package VLSys;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import VLSObjects.*;

public class TransactionExpense extends JPanel {
    
    IntegerField amount;
    JComboBox type;

    String[] typechoice = {"", "Operational", "Personal"};
/*
ImageIcon ask_red = new ImageIcon("images/prompt_ask.png");
ImageIcon notice = new ImageIcon("images/prompt_notice.png");
*/

    ImageIcon notice_green = new ImageIcon("images/prompt_notice_green.png");
    ImageIcon ask_red = new ImageIcon("images/prompt_ask_red.png");
    ImageIcon notice_red = new ImageIcon("images/prompt_notice_red.png");

    String blank = "";
    String string[] = new String[5];

    JButton submitButton;
    JButton cancelButton;

    JLabel amo;
    JLabel typ;
    JLabel addexp;

public TransactionExpense(final JFrame frame){
    
    setVisible(false);
    setLayout (null);
    setBackground(null);
    
    amount = new IntegerField();
    type = new JComboBox(typechoice);
    
    submitButton = new JButton();
    cancelButton = new JButton();
    
    amo = new JLabel ("Amount:");
    typ = new JLabel ("Type:");
    
    addexp = new JLabel ("ADD EXPENSE");
    addexp.setFont(new Font ("sansserif", Font.PLAIN, 40));
    addexp.setBounds(10,10,300,30);
    
    amo.setBounds(73,50,150,20);
    typ.setBounds(92,75,150,20);
    
    amount.setBounds(130,50,200,20);
    type.setBounds(130,75,200,20);
    
    amount.setBorder(null);
    type.setBorder(null);
    
    submitButton.setText("Submit");
    submitButton.setBounds(175,100,75,20);
    cancelButton.setText("Clear");
    cancelButton.setBounds(255,100,75,20);
    
    
    add(amount);
    add(type);
    add(amo);
    add(typ);
    add(submitButton);
    add(cancelButton);
    add(addexp);
    

    
    submitButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
                               
                string[0] = amount.getText();
                string[1] =(String)type.getSelectedItem();
                
                if(string[0].equals(blank) || string[1] == blank)
                {
                    checkEmpty();
 
                    JOptionPane.showMessageDialog(frame,
                                    "Incomplete Input",
                                    "Vehicle Logging Service",
                                    JOptionPane.WARNING_MESSAGE,
                                    notice_red);
                    
                }
                else if(string[0].length() > 8)
                {
                    checkEmpty();
 
                    JOptionPane.showMessageDialog(frame,
                                    "Invalid Input",
                                    "Vehicle Logging Service",
                                    JOptionPane.WARNING_MESSAGE,
                                    notice_red);
                    
                } 
                else if(string[0].equals(blank) == false && string[1] != blank)   
                {
                    int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Proceed with current values?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                                    ask_red);
                    System.out.print(n);
                    if(n==0)
                    {
                        Expense newExpense = new Expense();
                        ExpenseManager newEm = new ExpenseManager();
                        
  
                        newExpense.setExpenseDate(getTimestamp().toString().substring(0, 10));
                        newExpense.setExpenseType(string[1]);
                        newExpense.setExpenseAmount(Integer.parseInt(string[0]));
                        
                        
                      /* boolean existing = newSm.checkService(string[0], string[1], "--");
                       //check if a similar record already exists
                           
                       if(existing == true) 
                           {    JOptionPane.showMessageDialog(frame,
                                            "Vehicle record already exists!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice);
                           }
                           else
                           {   //add to database if no similar record is found
                              */ boolean result = newEm.addExpense(newExpense);
                               if (result == true)
                                    JOptionPane.showMessageDialog(frame,
                                            "Expense Added!",
                                            "Vehicle Logging Service",
                                            JOptionPane.WARNING_MESSAGE,
                                            notice_green);
                               resetValues();
                         //  }
                        }
                       else
                            {  System.out.print("ayaw!");

                            }
                    
                
                }
               
            }
        }
        );
    
    cancelButton.addActionListener(new  ActionListener()
       {
            public void actionPerformed(ActionEvent e)
            {
               int n = JOptionPane.showConfirmDialog(
                            frame,
                            "Are you sure you want to clear the values?",
                            "Vehicle Logging Service",
                            JOptionPane.YES_NO_OPTION,
                            JOptionPane.QUESTION_MESSAGE,
                                    ask_red);
               if(n==0)
                    {                
                        resetValues();
                    }
            }
        }
        );
            
    }

    public void checkEmpty()
    {   
        String s = "";
       
        if(amount.getText().length() == 0)
            amount.setBackground(Color.YELLOW);
        
        if(amount.getText().length() > 8)
            amount.setBackground(Color.RED);
        
        if(amount.getText().length() != 0)
            amount.setBackground(null);
        
        if((String)type.getSelectedItem() == s)
            type.setBackground(Color.YELLOW);
        
        if((String)type.getSelectedItem() != s)
            type.setBackground(null);
        
    }

    public void resetValues()
    {
                        amount.setBackground(null);
                type.setBackground(null);
                
                amount.setText("");
                type.setSelectedItem("");
    }
    
    public static java.sql.Timestamp getTimestamp() {
    java.util.Date today = new java.util.Date();
    return new java.sql.Timestamp(today.getTime());
    }
}
